const Utils = require('./Utils.js')

Utils.init()

var flag, count, gosts, storage
gosts = [
  [2570, 480, 80, 80],
  [2570, 790, 80, 80],
  [2570, 1100, 80, 80]
] // 3个鬼王坐标范围
flag = true
count = 0
storage = storages.create('yys')
challengeTheRegionalGhostKing()
/**
 * @description 挑战地域鬼王
 */
function challengeTheRegionalGhostKing() {
  const num = storage.get('num') || 3  // 要挑战的鬼王数量
  let times = 0 // 已挑战的鬼王数量
  while (times < num) {
    Utils.sleeps()
    let gw = gosts[times]
    if (Utils.findPicture('选择挑战')) {
      const x = random(gw[0], gw[0]+gw[2])
      const y = random(gw[1], gw[1]+gw[3])
      click(x, y)
      Utils.sleeps()
      let i = 1
      do {
        log("鬼王详情界面第 " + i + " 次")
        if (Utils.findPictureTap('挑战')) {
          Utils.waitTime(7)
          let flagTemp = true
          while (flagTemp) {
            if (Utils.findPictureTap('准备')) {
              flagTemp = false
            }
          }
          let seconds = random(40, 60)
          Utils.waitTime(seconds)
          let flagTempTwo = true
          while (flagTempTwo) {
            log('已等待：' + (seconds += 2) + '秒')
            if (Utils.findPictureTap('奖励')) {
              Utils.sleeps()
              Utils.findPictureTap('奖励')
              times++
              log('鬼王' + times + '：成功')
              flagTempTwo = false
            } else if (Utils.findPictureTap('胜利')) {
            } else if (Utils.findPicture('失败')) {
              Utils.stopJB()
            }
          }
          Utils.waitTime(8)
          Utils.findPictureTap('叉号', 0.8)
          i = 4
        }
        i++
        Utils.sleeps()
      } while (i < 5)
    } else {
      Utils.findPictureTap('筛选')
    }
  }
  Utils.sleeps()
  Utils.tapPicture('返回二')
  Utils.sleeps()
  Utils.tapPicture('返回二')
}